package ungdung_quanlymathang;

import java.util.List;

import com.example.quanly_mathang.R;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;


public class Ac_danhsachmathang extends Activity {
	Bundle bd2;
	Bundle bd;
	Intent it;
	ListView ls;
	CheckBox cb;
    ArrayAdapter<mat_hang> adapter;
    List<mat_hang> data;
    Cosodulieu csdl;
    mat_hang mh;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.danhsachmathang);
        it=new Intent(this.getApplicationContext(),Ac_themmathang.class);
        bd=new Bundle();
        bd2=this.getIntent().getExtras();
        TextView tx6=(TextView)findViewById(R.id.textView6); 
        tx6.setText(bd2.getString("ten_nguoidung")+"! Đăng xuất");
        mh=new mat_hang();
        ls=(ListView)findViewById(R.id.listView1);
        csdl=new Cosodulieu(this);
    	data=csdl.getall_mathang(Integer.valueOf(bd2.getString("idnd")));
    	adapter=new Adapter_customlist(this,data);
        ls.setAdapter(adapter);
        
               	           
    }

     
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_mathang, menu);
        return true;
    }
  
    public void dangxuat(View v){
    	Intent it=new Intent(this.getApplicationContext(),Ac_dangnhap.class);
    	startActivity(it);
    }
    public void them_mat_hang(View v)
    {
    		bd.putString("id_nd", bd2.getString("idnd"));
        	bd.putString("tennguoidung",bd2.getString("ten_nguoidung"));
        	bd.putString("mkhau", bd2.getString("matkhau"));
        	it.putExtras(bd);
        	startActivity(it);
        	
    }
    public void xoa_mat_hang(View v){
    	
			for(int i=0;i<ls.getCount();i++){
				View vi=ls.getChildAt(i);
				cb=(CheckBox)vi.findViewById(R.id.checkBox1);
				if(cb.isChecked()){
					csdl.xoa_mathang(data.get(i));
					adapter.notifyDataSetChanged();ls.refreshDrawableState();}
				}
			it=new Intent(this.getApplicationContext(),Ac_danhsachmathang.class);
			bd.putString("idnd", bd2.getString("idnd"));
        	bd.putString("ten_nguoidung",bd2.getString("ten_nguoidung"));
        	bd.putString("matkhau", bd2.getString("matkhau"));
        	it.putExtras(bd);
        	startActivity(it);
			}
    public void cap_nhat_mat_hang(View v){
    	for(int i=0;i<ls.getCount();i++){
			View vi=ls.getChildAt(i);
			cb=(CheckBox)vi.findViewById(R.id.checkBox1);
			if(cb.isChecked()){
				bd.putString("id_mh", String.valueOf(data.get(i).id_mathang));
				bd.putString("id_nd",String.valueOf(data.get(i).id_nguoidung));
				bd.putString("tennguoidung",bd2.getString("ten_nguoidung"));
				bd.putString("tenmathang", data.get(i).ten_mathang);
				bd.putString("giamathang", data.get(i).gia_mathang);
				bd.putString("nhasanxuat", data.get(i).nha_sanxuat);
				bd.putString("ngaycapnhat", data.get(i).ngay_capnhat);
				bd.putString("tennguoidung",bd2.getString("ten_nguoidung"));
				Intent it2=new Intent(this.getApplicationContext(),Ac_capnhatmathang.class);
				it2.putExtras(bd);
				startActivity(it2);
				
				}
			}
		
		
    }
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		if(item.getTitle().toString().equals("Thêm mặt hàng")){
			bd.putString("id_nd", bd2.getString("idnd"));
        	bd.putString("tennguoidung",bd2.getString("ten_nguoidung"));
        	bd.putString("mkhau", bd2.getString("matkhau"));
        	it.putExtras(bd);
        	startActivity(it);
        	return true;
        	
		}
		if(item.getTitle().toString().equals("Xóa mặt hàng")){
			for(int i=0;i<ls.getCount();i++){
				View v=ls.getChildAt(i);
				cb=(CheckBox)v.findViewById(R.id.checkBox1);
				if(cb.isChecked())
					{ 
						csdl.xoa_mathang(data.get(i));
						csdl.xoa_mathang(mh);
						adapter.notifyDataSetChanged();
					}
				ls.refreshDrawableState();
			}
							
			return true;}
		if(item.getTitle().toString().equals("Cập nhật mặt hàng")){
			for(int i=0;i<ls.getCount();i++){
				View vi=ls.getChildAt(i);
				cb=(CheckBox)vi.findViewById(R.id.checkBox1);
				if(cb.isChecked()){
					bd.putString("id_mh", String.valueOf(data.get(i).id_mathang));
					bd.putString("id_nd",String.valueOf(data.get(i).id_nguoidung));
					bd.putString("tennguoidung",bd2.getString("ten_nguoidung"));
					bd.putString("tenmathang", data.get(i).ten_mathang);
					bd.putString("giamathang", data.get(i).gia_mathang);
					bd.putString("nhasanxuat", data.get(i).nha_sanxuat);
					bd.putString("ngaycapnhat", data.get(i).ngay_capnhat);
					bd.putString("tennguoidung",bd2.getString("ten_nguoidung"));
					Intent it2=new Intent(this.getApplicationContext(),Ac_capnhatmathang.class);
					it2.putExtras(bd);
					startActivity(it2);
					
					}
				}
			
			return true;
		}
		
		
		return false;
	}
}